package logic

import (
	"context"
	"errors"
	"user_srv/models/mysql"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *user.UserRegisterRequest) (*user.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{
		Mobile: in.Mobile,
	}

	err := u.UserMobile(in.Mobile)
	if err != nil {
		logx.Info("检查用户已注册", err)
		return nil, errors.New("检查用户已注册")
	}
	//if u.Mobile == in.Mobile {
	//	logx.Info("检查用户已注册")
	//	return nil, errors.New("检查用户已注册")
	//}
	err = u.Create()
	if err != nil {
		logx.Info("注册用户信息失败", err)
		return nil, errors.New("注册用户信息失败")
	}

	return &user.UserRegisterResponse{
		RegisterId: int64(u.ID),
	}, nil
}
